package com.example.kingdee.service;

import cn.hutool.core.util.NumberUtil;
import com.example.kingdee.kingdeeBridge.dto.PaperDto;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaperCalculateService {

    public List<Map<String, Object>> sheetSize(PaperDto paperDto) {
        double croLength = paperDto.getCroLength();//横向长
        double cornerLength = paperDto.getCornerLength();//出血
        double lenLength = paperDto.getLenLength();//纵向长
        double croFixedLength = paperDto.getCroFixedLength();//规矩
        double lenFixedLength = paperDto.getLenFixedLength();//咬口
        List<Map<String, Object>> result = new ArrayList<>();
        for (int croIndex = 1; croIndex <= 30; croIndex++) {
            for (int lenIndex = 1; lenIndex <= 30; lenIndex++) {
                //横向总长（横向长*横向张数+出血设定*2*横向张数+规矩）< 1020
                double croAllLength = croLength*croIndex + cornerLength*2*croIndex + croFixedLength;
                //纵向总长（纵向长*纵向张数+出血设定*2*纵向张数+咬口方向设定）
                double lenAllLength = lenLength*lenIndex + cornerLength*2*lenIndex + lenFixedLength;
                if (280 <= croAllLength && croAllLength <= 1020 && 180 <= lenAllLength && lenAllLength <= 720){
                    if (croAllLength < 889){
                        Map<String, Object> divActiveAreaMap = new HashMap<>();
                        //纸张使用总面积
                        BigDecimal activeArea = NumberUtil.mul(croLength, croIndex, lenLength, lenIndex);
                        //纸张利用率: 纸张使用总面积 / 横向总长 / 纵向总长;
                        BigDecimal divActiveArea = NumberUtil.div(NumberUtil.div(activeArea, croAllLength), lenAllLength);
                        divActiveAreaMap.put("横向纸张数", croIndex);
                        divActiveAreaMap.put("纵向纸张数", lenIndex);
                        divActiveAreaMap.put("纸张利用率", divActiveArea);
                        result.add(divActiveAreaMap);
                    }
                }
            }
        }
        return result;
    }
}
